package com.zenithmind.user.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.user.pojo.domain.EmailLog;
import com.zenithmind.user.pojo.dto.EmailRequestDTO;
import com.zenithmind.user.pojo.query.EmailLogQuery;
import com.zenithmind.user.pojo.vo.EmailLogVO;
import com.zenithmind.user.service.EmailService;
import com.zenithmind.user.service.impl.EmailServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 邮件日志管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/email")
@Tag(name = "邮件管理")
public class EmailLogController {

    @Autowired
    private EmailService emailService;
    
    @Autowired
    private EmailServiceImpl emailServiceImpl;

    /**
     * 发送简单文本邮件
     *
     * @param emailRequestDTO 邮件请求DTO
     * @param request HTTP请求，用于获取IP地址
     * @return 邮件日志记录
     */
    @Log(description = "发送简单文本邮件", operationType = OperationTypeEnum.ADD)
    @PostMapping("/sendSimpleEmail")
    @Operation(summary = "发送简单文本邮件")
    public Result<EmailLog> sendSimpleEmail(
            @RequestBody EmailRequestDTO emailRequestDTO,
            HttpServletRequest request) {
        log.info("发送简单邮件请求: 收件人={}, 主题={}", 
                emailServiceImpl.maskEmail(emailRequestDTO.getTo()), 
                emailRequestDTO.getSubject());
                
        EmailLog emailLog = emailServiceImpl.sendSimpleEmail(emailRequestDTO, request);
        return Result.success(emailLog);
    }

    /**
     * 发送HTML邮件
     *
     * @param emailRequestDTO 邮件请求DTO
     * @param request HTTP请求，用于获取IP地址
     * @return 邮件日志记录
     */
    @Log(description = "发送HTML邮件", operationType = OperationTypeEnum.ADD)
    @PostMapping("/sendHtmlEmail")
    @Operation(summary = "发送HTML邮件")
    public Result<EmailLog> sendHtmlEmail(
            @RequestBody EmailRequestDTO emailRequestDTO,
            HttpServletRequest request) {
        log.info("发送HTML邮件请求: 收件人={}, 主题={}", 
                emailServiceImpl.maskEmail(emailRequestDTO.getTo()), 
                emailRequestDTO.getSubject());
                
        EmailLog emailLog = emailServiceImpl.sendHtmlEmail(emailRequestDTO, request);
        return Result.success(emailLog);
    }

    /**
     * 获取邮件发送历史
     *
     * @param emailRequestDTO 邮件请求DTO
     * @return 邮件日志列表
     */
    @Log(description = "查询邮件发送历史", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/getEmailHistory")
    @Operation(summary = "获取邮件发送历史")
    public Result<List<EmailLog>> getEmailHistory(@ModelAttribute EmailRequestDTO emailRequestDTO) {
        log.info("查询邮件历史: 邮箱={}, 数量={}", 
                emailServiceImpl.maskEmail(emailRequestDTO.getTo()), 
                emailRequestDTO.getLimit());
        List<EmailLog> history = emailService.getEmailHistory(emailRequestDTO);
        return Result.success(history);
    }
    
    /**
     * 分页查询邮件记录
     *
     * @param emailLogQuery 邮件查询条件，包含分页信息和查询条件
     * @return 分页邮件记录
     */
    @Log(description = "分页查询邮件记录", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/pageEmailLogs")
    @Operation(summary = "后台管理-分页查询邮件记录")
    public Result<PageResult<EmailLogVO>> pageEmailLogs(EmailLogQuery emailLogQuery) {
        log.info("分页查询邮件记录:{}", emailLogQuery);
        PageResult<EmailLogVO> result = emailService.pageEmailLogs(emailLogQuery);
        return Result.success(result);
    }
    
    /**
     * 根据ID获取邮件详情
     *
     * @param id 邮件ID
     * @return 邮件详情
     */
    @Log(description = "查询邮件详情", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/getEmailLogById/{id}")
    @Operation(summary = "获取邮件详情")
    public Result<EmailLog> getEmailLogById(@PathVariable @Parameter(description = "邮件ID") Long id) {
        log.info("查询邮件详情: ID={}", id);
        EmailLog emailLog = emailService.getById(id);
        return Result.success(emailLog);
    }
    
    /**
     * 更新邮件记录
     *
     * @param id 邮件ID
     * @param emailLog 邮件信息
     * @return 更新结果
     */
    @Log(description = "更新邮件记录", operationType = OperationTypeEnum.UPDATE)
    @PutMapping("/updateEmailLog/{id}")
    @Operation(summary = "更新邮件记录")
    public Result<Boolean> updateEmailLog(
            @PathVariable @Parameter(description = "邮件ID") Long id,
            @RequestBody EmailLog emailLog) {
        emailLog.setId(String.valueOf(id));
        log.info("更新邮件记录: ID={}", id);
        boolean success = emailService.updateById(emailLog);
        return Result.success(success);
    }
    
    /**
     * 删除邮件记录
     *
     * @param id 邮件ID
     * @return 删除结果
     */
    @Log(description = "删除邮件记录", operationType = OperationTypeEnum.DELETE)
    @DeleteMapping("/deleteEmailLog/{id}")
    @Operation(summary = "删除邮件记录")
    public Result<Boolean> deleteEmailLog(@PathVariable @Parameter(description = "邮件ID") Long id) {
        log.info("删除邮件记录: ID={}", id);
        boolean success = emailService.removeById(id);
        return Result.success(success);
    }
    
    /**
     * 批量删除邮件记录
     *
     * @param ids 邮件ID列表
     * @return 删除结果
     */
    @Log(description = "批量删除邮件记录", operationType = OperationTypeEnum.DELETE)
    @DeleteMapping("/batchDeleteEmailLogs")
    @Operation(summary = "批量删除邮件记录")
    public Result<Boolean> batchDeleteEmailLogs(@RequestBody List<Long> ids) {
        log.info("批量删除邮件记录: IDs={}", ids);
        boolean success = emailService.removeByIds(ids);
        return Result.success(success);
    }
    
    /**
     * 统计邮件发送情况
     *
     * @return 统计数据
     */
    @Log(description = "统计邮件发送情况", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/getEmailStatistics")
    @Operation(summary = "统计邮件发送情况")
    public Result<Object> getEmailStatistics() {
        log.info("统计邮件发送情况");
        Object statistics = emailService.getEmailStatistics();
        return Result.success(statistics);
    }
} 